import matplotlib.pyplot as plt
#import train

import ctypes

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

import segmentation.配置 as 配置

# import tkinter as tk


#配置中文显示
# import train

# plt.rcParams['font.family'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False


dirPath=配置.dirPath
modeName=配置.modeName

def printMaxMIOU():
    maxMIOU = 0
    model_path = dirPath+modeName+"/"
    allValLoss=[]
    allValMiou=[]
    with open(model_path+'评价指标.txt','a+') as f:    #�����ļ�����
        f.seek(0.0)#��ָ���Ƶ���ͷ
        while True:#��ȡ���е�����
            text = f.readline()  # ֻ��ȡһ������

            # �ж��Ƿ��ȡ������
            if not text:
                break

            texts=text.split(":")
            if texts[0].lower()=="loss":
                allValLoss.append(float(texts[1]))#��loss�ӵ�Loss�б��С�
            if texts[0].lower()=="miou":
                allValMiou.append(float(texts[1]))#��loss�ӵ�Loss�б��С�

    print("min value={},epoch is {}/{}".format(min(allValLoss),allValLoss.index(min(allValLoss))+1,len(allValLoss)) )
    print("max value={},epoch is {}/{}".format(max(allValMiou),allValMiou.index(max(allValMiou))+1,len(allValMiou)))


def draw(block=True):
    # root= tk.Tk()
    # root.withdraw()

    model_path = dirPath+modeName+"/"
    allValLoss=[]
    allValMiou=[]
    with open(model_path+'评价指标.txt','a+') as f:    #�����ļ�����
        f.seek(0.0)#��ָ���Ƶ���ͷ
        while True:#��ȡ���е�����
            text = f.readline()  # ֻ��ȡһ������

            # �ж��Ƿ��ȡ������
            if not text:
                break

            texts=text.split(":")
            if texts[0].lower()=="loss":
                allValLoss.append(float(texts[1]))#��loss�ӵ�Loss�б��С�
            if texts[0].lower()=="miou":
                allValMiou.append(float(texts[1]))#��loss�ӵ�Loss�б��С�



    X=range(len(allValMiou))
    #Y=allValLoss
    plt.figure(figsize=(12,5))

    plt.subplot(1,2,1)
    plt.xlabel("epoch")
    plt.ylabel("loss")
    plt.title("min value={},epoch is {}/{}".format(min(allValLoss),allValLoss.index(min(allValLoss))+1,len(allValLoss)))
    plt.plot(X,allValLoss)
    for x,y in zip(X,allValLoss):
        plt.text(x,y,y)

    plt.subplot(1,2,2)
    plt.xlabel("epoch")
    plt.ylabel("miou")
    plt.title("max value={},epoch is {}/{}".format(max(allValMiou),allValMiou.index(max(allValMiou))+1,len(allValMiou)))
    plt.plot(X,allValMiou)
    for x,y in zip(X,allValMiou):
        plt.text(x,y,y)




    # canvas=FigureCanvasTkAgg(plt.Figure(),master=root)
    # canvas.draw()
    # canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
    # root.mainloop()

    #plt.plot(X,allValMiou)
    plt.suptitle(model_path)
    plt.show(block=block)

if __name__ == '__main__':
    draw()
